﻿using Bigger.Demo.DbModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bigger.Demo.DbProviders.Implements
{
    public class ProductProvider : IProductProvider
    {
        private static IList<PruductModel> pruducts = new List<PruductModel>();
        static ProductProvider()
        {
            string[] productNames = new string[] { "橙子", "橘子", "苹果", "香蕉", "雪莉", "西瓜", "草莓", "葡萄", "奇异果", "哈密瓜", "火龙果" };
            decimal[] productPrices = new decimal[] { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
            string description = "abcdefghijklmnopqrsduvwxyz0123456";

            Random random = new Random();

            for (int i = 1; i <= 100; i++)
            {
                PruductModel pruduct = new PruductModel
                {
                    ProductId = i,
                    ProductName = $"{productNames[random.Next(0, productNames.Length)]}-{i}",
                    ProductPrice = productPrices[random.Next(0, productPrices.Length)],
                    StockQty = random.Next(0, 101),
                    Description = description.Substring(random.Next(0, description.Length - 5), 5)
                };
                pruducts.Add(pruduct);
            }
        }

        /// <summary>
        /// 获取商品列表
        /// </summary>
        /// <returns></returns>
        public IList<PruductModel> GetList()
        {
            return pruducts;
        }
        /// <summary>
        /// 分页获取商品列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public IList<PruductModel> GetPageList(int pageIndex, int pageSize)
        {
            return pruducts.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
        }
        /// <summary>
        /// 分页获取商品列表并获取总数
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public IList<PruductModel> GetPageList(int pageIndex, int pageSize, out long recordCount)
        {
            recordCount = this.Count();
            return this.GetPageList(pageIndex, pageSize);
        }
        /// <summary>
        /// 根据商品编号获取商品
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public PruductModel GetItem(int productId)
        {
            return pruducts.FirstOrDefault(c => c.ProductId == productId);
        }
        /// <summary>
        /// 获取商品总数
        /// </summary>
        /// <returns></returns>
        public long Count()
        {
            return pruducts.Count;
        }
    }
}
